$(function(){
	$('#btn').bind('click', function() {
		if(checkCode() == false){
			return false;
		}else if(checkStrLength($("#password").val(), 6, 16)  == false){
			return false;
		}else{
			submitCommit();
		}
	});
   $('#checkcode').bind('change', function() {
		checkCode();
	});
   
	$('#password').bind('change', function() {
		checkStrLength($("#password").val(), 6, 16);
	});
	$('#buyerName').bind('change', function() {
		chinese($("#buyerName").val());
	});
	
});

function checkCode() {
	var codeUuid = localStorage.getItem("phoneNoUUID");
	var code = $("input[name='code']").val();
	$.ajax({
		type : "POST",
		dataType : 'json',
		async:false,
		url : server_url + 'rest/frontUser/checkCode.do?',
		data : {
			codeUuid : codeUuid,
			code : code,
			ran:Math.random()
		},
		success : function(data) {
			if (data == 1) {
				return true;
			} else if (data == 0){
			alert("验证码错误");
			return false;
			}
		}
	});
}

function checkStrLength(userPass, min, max) {// 验证密码
	if(userPass == null || userPass == ''){
		alert("请输入密码!");
	}
	if (userPass.length < min || userPass.length > max || userPass == "") {
		alert("输入字符长度须在" + min + "-" + max + "之间!");
		return false;
	} else {
		return  true;
	}
}
function chinese(china) { // 验证只能是中文
	var pattern = /^[\u4e00-\u9fa5]*$/;
	if (!pattern.test(china) || china == "") {
		alert("名称不能为空且必须为中文");
		return false;
	} else {
		return  true;
	}
}
function submitCommit() {
	$.ajax({
		url: server_url + "/rest/supply/register/getUser.do?ran=" + Math.random(),
		data: $("form").serializeArray(),
		success: function (data) {
			if (data.retCode == '0002') {
				alert("该账号已被注册");
				return;
			}
			if (data.retCode == '0001') {
				orginalAlert("该账号已经注册卖家，卖家密码将被更新");
			}
			$.ajax({
				type: 'post',
				async: false,
				dataType: 'json',
				url: server_url + 'rest/frontUser/buyerAdd.do?ran=' + Math.random(),
				data: $("#buyer").serializeArray(),
				contentType: "application/x-www-form-urlencoded; charset=utf-8",
				success: function (data) {
					if (data.retCode == "0000") {
						countDown(3);
					} else {
						alert("信息有误，请重新输入");
					}
				}
			});
		}
	})
}    
function countDown(secconds){      
   alert("注册成功");     
    if(--secconds>0){         
         setTimeout("countDown("+secconds+")",1000);         
     }else{           
    	 window.location.href = server_url+"login.do";         
     }         
 }  